const {playSong} =require("../../models/playSong")
const audio = wx.getBackgroundAudioManager();
Page({
  data: {
    play:"",
    state:true
  },
  onLoad: function (options) {
    var {id} = options
    var imgUrl = wx.getStorageSync('url')
    var name = wx.getStorageSync('name')
    
    playSong(id).then(res=>{
      var data= res.data.data[0].url
      audio.title = name
      audio.src= data
      let states = wx.getStorageSync('states')
      // 判断是否有缓存
      if(states){
        states[id]=true
        /* for(let key in states){
          if(key != id){
            states[key] = false
          }
        } */
        wx.setStorageSync('states', states)
      }else{
        let states ={}
        states[id] = true
        wx.setStorageSync('states', states)
      }
      this.setData({
        play:{
          data,
          imgUrl,
          name,
          id
        }
      })
    })
  },
  handlePlay(){
    var playStates = wx.getStorageSync('states')
    var id = this.data.play.id
    playStates[id] = !playStates[id]
    this.setData({
      state:playStates[id]
    })
    wx.setStorageSync('states', playStates)
    if(playStates[id]){
      audio.play()
    }else{
      audio.pause()
    }
  }
})